package niconama04.client;

import java.util.List;

import niconama04.shared.data.Category;
import niconama04.shared.data.CategoryOriginal;
import niconama04.shared.data.Grammer;
import niconama04.shared.data.GrammerWord;
import niconama04.shared.data.Original;
import niconama04.shared.data.Word;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;


public class Reference implements EntryPoint {

	/** Create a remote service proxy to talk to the server-side service. */
	final ReferenceServiceAsync service = GWT.create(ReferenceService.class);

	Button btnG;

	Button btnW;

	Button btnO;

	Button btnC;

	Button btnGW;

	Button btnCO;

	Label label;

	FlexTable table;

	/**
	 * This is the entry point method.
	 */
	@Override
	public void onModuleLoad() {

		ViewBuilder.build();

		RootPanel rp = RootPanel.get("gwt_reference");
		if (rp == null) {
			return;
		}

		label = new Label();
		table = new FlexTable();
		table.setBorderWidth(3);
		table.setCellPadding(5);

		{
			VerticalPanel vp = new VerticalPanel();
			{
				rp.add(vp);
			}
			{
				HorizontalPanel buttons = new HorizontalPanel();
				btnG = new Button("Grammer");
				btnW = new Button("Word");
				btnO = new Button("Original");
				btnC = new Button("Category");
				btnGW = new Button("GrammerWord");
				btnCO = new Button("CategoryOriginal");
				buttons.add(btnG);
				buttons.add(btnW);
				buttons.add(btnO);
				buttons.add(btnC);
				buttons.add(btnGW);
				buttons.add(btnCO);
				vp.add(buttons);
			}
			{
				Label sep = new Label("");
				sep.setHeight("10px");
				vp.add(sep);
				vp.add(label);
				vp.add(table);
			}
		}
		{
			btnG.addClickHandler(new ClickHandler() {
				
				@Override
				public void onClick(ClickEvent x_event) {
					loadGrammer();
				}
			});
			btnW.addClickHandler(new ClickHandler() {
				
				@Override
				public void onClick(ClickEvent x_event) {
					loadWord();
				}
			});
			btnO.addClickHandler(new ClickHandler() {
				
				@Override
				public void onClick(ClickEvent x_event) {
					loadOriginal();
				}
			});
			btnC.addClickHandler(new ClickHandler() {
				
				@Override
				public void onClick(ClickEvent x_event) {
					loadCategory();
				}
			});
			btnGW.addClickHandler(new ClickHandler() {
				
				@Override
				public void onClick(ClickEvent x_event) {
					loadGrammerWord();
				}
			});
			btnCO.addClickHandler(new ClickHandler() {
				
				@Override
				public void onClick(ClickEvent x_event) {
					loadCategoryOriginal();
				}
			});
		}
		{
			loadGrammer();
		}
	}

	void loadGrammer() {
		service.getGrammers(new AsyncCallback<List<Grammer>>(){

			@Override
			public void onFailure(Throwable x_caught) {
				label.setText("Grammer");
				table.removeAllRows();
				table.setWidget(0, 0, new Label(x_caught.getMessage()));
			}

			@Override
			public void onSuccess(List<Grammer> x_result) {
				label.setText("Grammer");
				table.removeAllRows();
				int row = table.getRowCount();
				table.setWidget(row, 0, new Label("key"));
				table.setWidget(row, 1, new Label("configuration"));
				row++;
				for (Grammer grammer : x_result) {
					table.setWidget(row, 0, new Label(String.valueOf(grammer.getKey())));
					table.setWidget(row, 1, new Label(grammer.getConfiguration()));
					row++;
				}
			}
		});
	}

	void loadWord() {
		service.getWords(new AsyncCallback<List<Word>>(){

			@Override
			public void onFailure(Throwable x_caught) {
				label.setText("Word");
				table.removeAllRows();
				table.setWidget(0, 0, new Label(x_caught.getMessage()));
			}

			@Override
			public void onSuccess(List<Word> x_result) {
				label.setText("Word");
				table.removeAllRows();
				int row = table.getRowCount();
				table.setWidget(row, 0, new Label("key"));
				table.setWidget(row, 1, new Label("surface"));
				table.setWidget(row, 2, new Label("reading"));
				table.setWidget(row, 3, new Label("pos"));
				table.setWidget(row, 4, new Label("baseform"));
				table.setWidget(row, 5, new Label("originalKey"));
				row++;
				for (Word word : x_result) {
					table.setWidget(row, 0, new Label(String.valueOf(word.getKey())));
					table.setWidget(row, 1, new Label(word.getSurface()));
					table.setWidget(row, 2, new Label(word.getReading()));
					table.setWidget(row, 3, new Label(word.getPos().getName()+"("+word.getPos().getKey()+")"));
					table.setWidget(row, 4, new Label(word.getBaseform()));
					table.setWidget(row, 5, new Label(String.valueOf(word.getOriginalKey())));
					row++;
				}
			}
		});
	}

	void loadOriginal() {
		service.getOriginals(new AsyncCallback<List<Original>>(){

			@Override
			public void onFailure(Throwable x_caught) {
				label.setText("Original");
				table.removeAllRows();
				table.setWidget(0, 0, new Label(x_caught.getMessage()));
			}

			@Override
			public void onSuccess(List<Original> x_result) {
				label.setText("Original");
				table.removeAllRows();
				int row = table.getRowCount();
				table.setWidget(row, 0, new Label("key"));
				table.setWidget(row, 1, new Label("pos"));
				table.setWidget(row, 2, new Label("baseform"));
				row++;
				for (Original original : x_result) {
					table.setWidget(row, 0, new Label(String.valueOf(original.getKey())));
					table.setWidget(row, 1, new Label(original.getPos().getName()+"("+original.getPos().getKey()+")"));
					table.setWidget(row, 2, new Label(original.getBaseform()));
					row++;
				}
			}
		});
	}

	void loadCategory() {
		service.getCategorys(new AsyncCallback<List<Category>>(){

			@Override
			public void onFailure(Throwable x_caught) {
				label.setText("Category");
				table.removeAllRows();
				table.setWidget(0, 0, new Label(x_caught.getMessage()));
			}

			@Override
			public void onSuccess(List<Category> x_result) {
				label.setText("Category");
				table.removeAllRows();
				int row = table.getRowCount();
				table.setWidget(row, 0, new Label("key"));
				table.setWidget(row, 1, new Label("name"));
				row++;
				for (Category category : x_result) {
					table.setWidget(row, 0, new Label(String.valueOf(category.getKey())));
					table.setWidget(row, 1, new Label(category.getName()));
					row++;
				}
			}
		});
	}

	void loadGrammerWord() {
		service.getGrammerWords(new AsyncCallback<List<GrammerWord>>(){

			@Override
			public void onFailure(Throwable x_caught) {
				label.setText("GrammerWord");
				table.removeAllRows();
				table.setWidget(0, 0, new Label(x_caught.getMessage()));
			}

			@Override
			public void onSuccess(List<GrammerWord> x_result) {
				label.setText("GrammerWord");
				table.removeAllRows();
				int row = table.getRowCount();
				table.setWidget(row, 0, new Label("key"));
				table.setWidget(row, 1, new Label("grammerKey"));
				table.setWidget(row, 2, new Label("wordKey"));
				table.setWidget(row, 3, new Label("index"));
				row++;
				for (GrammerWord gw : x_result) {
					table.setWidget(row, 0, new Label(String.valueOf(gw.getKey())));
					table.setWidget(row, 1, new Label(String.valueOf(gw.getGrammerKey())));
					table.setWidget(row, 2, new Label(String.valueOf(gw.getWordKey())));
					table.setWidget(row, 3, new Label(String.valueOf(gw.getIndex())));
					row++;
				}
			}
		});
	}

	void loadCategoryOriginal() {
		service.getCategoryOriginals(new AsyncCallback<List<CategoryOriginal>>(){

			@Override
			public void onFailure(Throwable x_caught) {
				label.setText("CategoryOriginal");
				table.removeAllRows();
				table.setWidget(0, 0, new Label(x_caught.getMessage()));
			}

			@Override
			public void onSuccess(List<CategoryOriginal> x_result) {
				label.setText("CategoryOriginal");
				table.removeAllRows();
				int row = table.getRowCount();
				table.setWidget(row, 0, new Label("key"));
				table.setWidget(row, 1, new Label("categoryKey"));
				table.setWidget(row, 2, new Label("originalKey"));
				row++;
				for (CategoryOriginal cw : x_result) {
					table.setWidget(row, 0, new Label(String.valueOf(cw.getKey())));
					table.setWidget(row, 1, new Label(String.valueOf(cw.getCategoryKey())));
					table.setWidget(row, 2, new Label(String.valueOf(cw.getOriginalKey())));
					row++;
				}
			}
		});
	}
}
